﻿///////////////////////////////////////////////////////////////////
//项目：基于手机蓝牙的可配置的计算机应用程序遥控
//作者：华南理工：曹炼强 林伟彬
//本文件功能：程序按键配置窗口，用于配置应用程序及其对应按键信息
//////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace BCC
{
    public partial class SetForm : Form
    {
        private xmlClass xml = new xmlClass();
        public string fileName = "";
        private string appName = "";
        //private bool isPressed = true;
        public SetForm()
        {
            InitializeComponent();
        }
        private void setForm_Load(object sender, EventArgs e)
        {
            int i = 0;
            label_fileName.Text = fileName+"中程序";
            //xmlClass xml = new xmlClass();
            GetIconClass geticon = new GetIconClass();
            DataSet ds = xml.readXml(fileName);
            if (ds == null)
            {
                MessageBox.Show(fileName+"中内容被恶意破坏，请删除重建");
                this.Close();
                this.Dispose();
                return;
            }
            listView1.Items.Clear();
            try
            {
                while (i < ds.Tables["Node"].Rows.Count)
                {
                    string name = ds.Tables["Node"].Rows[i]["程序名称"].ToString();
                    string address = ds.Tables["Node"].Rows[i]["程序路径"].ToString();
                    Icon theicon = geticon.GetIconByFileName(address);
                    if(theicon==null)
                    {
                        theicon = geticon.GetIconByFileType(".exe", false);
                        if (theicon == null)
                            imageList1.Images.Add(new Icon("\\img\\16-1.ICO") );
                        else
                            imageList1.Images.Add(theicon);
                    }
                    
                    else
                    imageList1.Images.Add((Icon)theicon);
                    listView1.Items.Add(name,imageList1.Images.Count-1);
                    i++;
                }
                appName = ds.Tables["Node"].Rows[0]["程序名称"].ToString();
                loadDatagridviewData(appName);
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show(fileName+"中尚无内容，请添加");
            }
            

        }
        private void listView1_changed(object sender,EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;
            appName = listView1.SelectedItems[0].Text;
            loadDatagridviewData(appName);
        }
        private void loadDatagridviewData(string name)
        {
            label_appName.Text = name + "的配置文件";            
            DataSet ds=xml.readXml(fileName);
            int x = 0, j = ds.Tables["Node"].Rows.Count;
            while (x < j)
            {
                if (ds.Tables["Node"].Rows[x]["程序名称"].ToString() == name)
                {
                    break;
                }
                x++;
            }
            DataTable dt = new DataTable("配置");                       //建立个数据表
            dt.Columns.Add(new DataColumn("事件", typeof(string)));       //在表中添加string类型的列 
            dt.Columns.Add(new DataColumn("配置键", typeof(string)));   //在表中添加string类型的Name列 
            dt.Columns.Add(new DataColumn("事件.", typeof(string)));      //在表中添加string类型的列,不可以同名，故加多个点 
            dt.Columns.Add(new DataColumn("配置键.", typeof(string)));  //在表中添加string类型的Name列,不可以同名，故加多个点 
            DataRow dr;//行 
            for (int i = 0; i < 9; i++)
            {
                dr = dt.NewRow();
                dr["事件"] = inttostr(i * 2);
                dr["配置键"] = ds.Tables["Node"].Rows[x][i * 2];
                dr["事件."] = inttostr(i*2 + 1);
                dr["配置键."] = ds.Tables["Node"].Rows[x][i*2+1];
                dt.Rows.Add(dr);                                        //在表的对象的行里添加此行 
            }
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
            //dataGridView1[3, 8].ReadOnly = false;
            
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            
        }
        

        private void button_save_Click(object sender, EventArgs e)
        {
            if (!xml.searchNode(fileName ,appName))
            {
                MessageBox.Show("配置文件被破坏，请重建");
            }
            for (int i = 0; i < 8; i++)
            {
                if (xml.searchNodeElement(fileName, appName, inttokey(2 * i)))
                    xml.updateNodeElement(fileName, appName, inttokey(2 * i), dataGridView1[1, i].Value.ToString());
                else 
                    xml.addNodeElement(fileName, appName, inttokey(2 * i), dataGridView1[1, i].Value.ToString());
                if (xml.searchNodeElement(fileName, appName, inttokey(2 * i + 1)))
                    xml.updateNodeElement(fileName, appName, inttokey(2 * i + 1), dataGridView1[3, i].Value.ToString());
                else 
                    xml.addNodeElement(fileName, appName, inttokey(2 * i + 1), dataGridView1[3, i].Value.ToString());
            }
            if (xml.searchNodeElement(fileName, appName, inttokey(16)))
                xml.updateNodeElement(fileName, appName, inttokey(16), dataGridView1[1, 8].Value.ToString());
            else
                xml.addNodeElement(fileName, appName, inttokey(16), dataGridView1[1, 8].Value.ToString());
            xml.updateName(fileName, appName, dataGridView1[3, 8].Value.ToString());
            MessageBox.Show("保存成功！");
            setForm_Load(sender,e);
            
        }

        private void button_cansle_Click(object sender, EventArgs e)
        {
            setForm_Load(sender, e);
        }

        private void button_addapp_Click(object sender, EventArgs e)
        {
            string appAddress = "";
            openFileDialog1.InitialDirectory = @"E:\\";
            openFileDialog1.Filter = "幻灯片文件(*.ppt,*.pptx)|*.ppt;*.pptx|Word文档(*.doc,*.docx)|*.doc;*.docx|Excel文档(*.xls,*.xlsx)|*.xls;*.xlsx|可执行文件(*.exe)|*.exe";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                appAddress = openFileDialog1.FileName;
                if (appAddress == "") return;
            }
            else { return; }
            string[] sArray = appAddress.Split('\\');
            int x = sArray.Length;
            appName = sArray[x-1];
            GetIconClass getIcon = new GetIconClass();
            Icon thefileicon = getIcon.GetIconByFileName(appAddress);
            xml.addXmlNode(fileName, appName, appAddress);
            imageList1.Images.Add(thefileicon);
            listView1.Items.Add(appName, imageList1.Images.Count - 1);

            int lenappName = appName.Length;
            string str1 = appName.Substring(lenappName - 3, 3);
            string str2 = appName.Substring(lenappName - 4, 4);
            string m_appName = appName.ToLower();
            if (str1 == "ppt" || str2 == "pptx")
            {
                xml.updateNodeElement(fileName, appName, "zero", "Up");
                xml.updateNodeElement(fileName, appName, "one", "Down");
                xml.updateNodeElement(fileName, appName, "two", "Left");
                xml.updateNodeElement(fileName, appName, "three", "Right");
                xml.updateNodeElement(fileName, appName, "four", "Up");
                xml.updateNodeElement(fileName, appName, "five", "Down");
                xml.updateNodeElement(fileName, appName, "six", "Left");
                xml.updateNodeElement(fileName, appName, "seven", "Right");
                xml.updateNodeElement(fileName, appName, "eight", "F5");
                xml.updateNodeElement(fileName, appName, "nine", "Shift+F5");
                xml.updateNodeElement(fileName, appName, "ok", "Escape");
                xml.updateNodeElement(fileName, appName, "up", "Return");
                xml.updateNodeElement(fileName, appName, "down", "Alt+F4");
            }
            else if (m_appName.Contains("kwmusic"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+F5");
                xml.updateNodeElement(fileName, appName, "one", "Control+F6");
                xml.updateNodeElement(fileName, appName, "two", "Control+Alt+S");
                xml.updateNodeElement(fileName, appName, "three", "Control+Shift+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Shift+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Shift+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Shift+Down");
            }
            else if (m_appName.Contains("qqmusic"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+Alt+F5");
                xml.updateNodeElement(fileName, appName, "one", "");
                xml.updateNodeElement(fileName, appName, "two", "");
                xml.updateNodeElement(fileName, appName, "three", "Control+Alt+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Alt+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Alt+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Alt+Down");
            }
            else if (m_appName.Contains("ttplayer"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+Alt+F5");
                xml.updateNodeElement(fileName, appName, "one", " Control+Alt+F6");
                xml.updateNodeElement(fileName, appName, "two", "Control+Alt+S");
                xml.updateNodeElement(fileName, appName, "three", "Control+Alt+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Alt+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Alt+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Alt+Down");
            }
            else if (m_appName.Contains("kugou") || m_appName.Contains("kugoo"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+Alt+F5");
                xml.updateNodeElement(fileName, appName, "one", "");
                xml.updateNodeElement(fileName, appName, "two", "Control+Alt+S");
                xml.updateNodeElement(fileName, appName, "three", "Control+Alt+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Alt+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Alt+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Alt+Down");
            }
            else if (str1 == "exe")
            {
                xml.updateNodeElement(fileName, appName, "zero", "Up");
                xml.updateNodeElement(fileName, appName, "one", "Down");
                xml.updateNodeElement(fileName, appName, "two", "Left");
                xml.updateNodeElement(fileName, appName, "three", "Right");
                xml.updateNodeElement(fileName, appName, "four", "");
                xml.updateNodeElement(fileName, appName, "five", "");
                xml.updateNodeElement(fileName, appName, "six", "");
                xml.updateNodeElement(fileName, appName, "seven", "");
                xml.updateNodeElement(fileName, appName, "eight", "");
                xml.updateNodeElement(fileName, appName, "nine", "");
            }
            else
            {
                xml.updateNodeElement(fileName, appName, "zero", "Up");
                xml.updateNodeElement(fileName, appName, "one", "Down");
                xml.updateNodeElement(fileName, appName, "two", "Left");
                xml.updateNodeElement(fileName, appName, "three", "Right");
                xml.updateNodeElement(fileName, appName, "four", "Up");
                xml.updateNodeElement(fileName, appName, "five", "Down");
                xml.updateNodeElement(fileName, appName, "six", "Left");
                xml.updateNodeElement(fileName, appName, "seven", "Right");
                xml.updateNodeElement(fileName, appName, "eight", "Return");
                xml.updateNodeElement(fileName, appName, "nine", "Alt+F4");
            }
            loadDatagridviewData(appName);
        }

        private void button_detapp_Click(object sender, EventArgs e)
        {
            try
            {
                string theName = listView1.SelectedItems[0].Text;
                DialogResult dr = MessageBox.Show("你确定删除" + theName + "的配置信息？", "配置程序删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if ((int)dr == 1)
                {
                    xml.deletXmlNode(fileName, theName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("请先选择一个你要删除的程序","错误警告",MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally {
                
                setForm_Load(sender, e);            
            }
        }
        private bool canRead=false;
        private void cellClicked(object sender, DataGridViewCellEventArgs e)
        {
            canRead = true;
        }
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }
        private void keyup(object sender, KeyEventArgs ke)
        {
            //ke.Handled = true;
            while (canRead == true)
            {
                string thekey = "";
                bool isAlt = ke.Alt;
                bool isCtrl = ke.Control;
                bool isShift = ke.Shift;
                if (isCtrl)
                    thekey += "Control+";
                if (isAlt)
                    thekey += "Alt+";
                if (isShift)
                    thekey += "Shift+";
                thekey += ke.KeyCode.ToString();
                int row = dataGridView1.CurrentCell.RowIndex;
                int column = dataGridView1.CurrentCell.ColumnIndex;
                //if (thekey.Equals("Left")&&(column!=0))
                //{
                //    column = column + 1;

                //}
                //if (thekey.Equals("Right") && (column != 3))
                //{
                //    column = column - 1;
                //}
                //if (thekey.Equals("Up") && (row != 0))
                //{
                //    row = row + 1;
                //}
                //if (thekey.Equals("Down") && (row != 8))
                //{
                //    row = row - 1;
                //}
                //if (thekey.Equals("Tab"))
                //{
                //    if (column == 0)
                //    {
                //        column = 3;
                //        row -= 1;
                //    }
                //    else {
                //        column -= 1;
                //    }
                //}
                
                //if ((column == 2) && (row == 8))
                //{
                //    dataGridView1[2, 8].ToolTipText = "good";
                //}
                //if ((column == 1) || (column == 3))
                //{
                //    dataGridView1[column, row].Value = thekey;
                //}
                //if ((column == 0) || ((column == 2) && (row != 8)))
                //{
                //    dataGridView1[column + 1, row].Value = thekey;
                //}

                if (column == 0 || column == 1)
                {
                    string str = inttostr(row * 2);
                    if (str == "" || str == "程序名称")
                    {
                        canRead = false;
                        break;
                    }

                }

                if (column == 2 || column == 3)
                {
                    string str = inttostr(row * 2 + 1);
                    if (str == "" || str == "程序名称")
                    {
                        canRead = false;
                        break;
                    }
                }

                if (column == 0 || column == 1)
                    dataGridView1[1, row].Value = thekey;

                if (column == 2 || column == 3)
                    dataGridView1[3, row].Value = thekey;

                canRead = false;
            }
            //
            
            
        }
        private string inttostr(int i)
        {
            int lenappName = appName.Length;
            string str1 = appName.Substring(lenappName - 3, 3);
            string str2 = appName.Substring(lenappName - 4, 4);
            if (str1 == "ppt" || str2 == "pptx")
            {
                if (i == 0) return "向上滑";
                if (i == 1) return "向下滑";
                if (i == 2) return "向左滑";
                if (i == 3) return "向右滑";
                if (i == 4) return "单击向上";
                if (i == 5) return "单击向下";
                if (i == 6) return "单击向左";
                if (i == 7) return "单击向右";
                if (i == 8) return "从头放映";
                if (i == 9) return "当前放映";
                if (i == 10) return "退出放映";
                if (i == 11) return "Enter键";
                if (i == 12) return "关闭程序";
                if (i == 13) return "";
                if (i == 14) return "";
                if (i == 15) return "";
                if (i == 16) return "";
                if (i == 17) return "程序名称";
                else return "?";
            }

            string m_appName = appName.ToLower();
            if (m_appName.Contains("kugoo") || m_appName.Contains("kugou") || m_appName.Contains("kwmusic") || m_appName.Contains("ttplayer") || m_appName.Contains("qqmusic"))
            {
                if (i == 0) return "播放/暂停";
                if (i == 1) return "停止";
                if (i == 2) return "静音";
                if (i == 3) return "上一首";
                if (i == 4) return "下一首";
                if (i == 5) return "增大音量";
                if (i == 6) return "减小音量";
                if (i == 7) return "";
                if (i == 8) return "";
                if (i == 9) return "";
                if (i == 10) return "";
                if (i == 11) return "";
                if (i == 12) return "";
                if (i == 13) return "";
                if (i == 14) return "";
                if (i == 15) return "";
                if (i == 16) return "";
                if (i == 17) return "程序名称";
                else return "?";
            }
            if (str1 == "exe")
            {
                if (i == 0) return "向上";
                if (i == 1) return "向下";
                if (i == 2) return "向左";
                if (i == 3) return "向右";
                if (i == 4) return "A键";
                if (i == 5) return "B键";
                if (i == 6) return "连发A键";
                if (i == 7) return "连发B键";
                if (i == 8) return "开始";
                if (i == 9) return "选择";
                if (i == 10) return "";
                if (i == 11) return "";
                if (i == 12) return "";
                if (i == 13) return "";
                if (i == 14) return "";
                if (i == 15) return "";
                if (i == 16) return "";
                if (i == 17) return "程序名称";
                else return "?";
            }
            if (i == 0) return "向上滑";
            if (i == 1) return "向下滑";
            if (i == 2) return "向左滑";
            if (i == 3) return "向右滑";
            if (i == 4) return "单击向上";
            if (i == 5) return "单击向下";
            if (i == 6) return "单击向左";
            if (i == 7) return "单击向右";
            if (i == 8) return "Enter键";
            if (i == 9) return "关闭程序";
            if (i == 10) return "";
            if (i == 11) return "";
            if (i == 12) return "";
            if (i == 13) return "";
            if (i == 14) return "";
            if (i == 15) return "";
            if (i == 16) return "";
            if (i == 17) return "程序名称";
            else return "?";
        }
        private string inttokey(int i)
        {
            if (i == 0) return "zero";
            if (i == 1) return "one";
            if (i == 2) return "two";
            if (i == 3) return "three";
            if (i == 4) return "four";
            if (i == 5) return "five";
            if (i == 6) return "six";
            if (i == 7) return "seven";
            if (i == 8) return "eight";
            if (i == 9) return "nine";
            if (i == 10) return "ok";
            if (i == 11) return "up";
            if (i == 12) return "down";
            if (i == 13) return "left";
            if (i == 14) return "right";
            if (i == 15) return "xing";
            if (i == 16) return "jing";
            if (i == 17) return "name";
            else return "wrong";
        }

        public void SetForm_Disposed(object sender,EventArgs e)
        {
        }

        private void button_SetDefault_Click(object sender, EventArgs e)
        {
            int lenappName = appName.Length;
            string str1 = appName.Substring(lenappName - 3, 3);
            string str2 = appName.Substring(lenappName - 4, 4);
            string m_appName = appName.ToLower();

            if (str1 == "ppt" || str2 == "pptx")
            {
                xml.updateNodeElement(fileName, appName, "zero", "Up");
                xml.updateNodeElement(fileName, appName, "one", "Down");
                xml.updateNodeElement(fileName, appName, "two", "Left");
                xml.updateNodeElement(fileName, appName, "three", "Right");
                xml.updateNodeElement(fileName, appName, "four", "Up");
                xml.updateNodeElement(fileName, appName, "five", "Down");
                xml.updateNodeElement(fileName, appName, "six", "Left");
                xml.updateNodeElement(fileName, appName, "seven", "Right");
                xml.updateNodeElement(fileName, appName, "eight", "F5");
                xml.updateNodeElement(fileName, appName, "nine", "Shift+F5");
                xml.updateNodeElement(fileName, appName, "ok", "Escape");
                xml.updateNodeElement(fileName, appName, "up", "Return");
                xml.updateNodeElement(fileName, appName, "down", "Alt+F4");
            }
            else if(m_appName.Contains("kwmusic"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+F5");
                xml.updateNodeElement(fileName, appName, "one", "Control+F6");
                xml.updateNodeElement(fileName, appName, "two", "Control+Alt+S");
                xml.updateNodeElement(fileName, appName, "three", "Control+Shift+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Shift+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Shift+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Shift+Down");
            }
            else if (m_appName.Contains("qqmusic"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+Alt+F5");
                xml.updateNodeElement(fileName, appName, "one", "");
                xml.updateNodeElement(fileName, appName, "two", "");
                xml.updateNodeElement(fileName, appName, "three", "Control+Alt+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Alt+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Alt+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Alt+Down");
            }
            else if (m_appName.Contains("ttplayer"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+Alt+F5");
                xml.updateNodeElement(fileName, appName, "one", " Control+Alt+F6");
                xml.updateNodeElement(fileName, appName, "two", "Control+Alt+S");
                xml.updateNodeElement(fileName, appName, "three", "Control+Alt+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Alt+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Alt+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Alt+Down");
            } //KuGoo.exe kugou7.exe
            else if (m_appName.Contains("kugou") || m_appName.Contains("kugoo"))
            {
                xml.updateNodeElement(fileName, appName, "zero", "Control+Alt+F5");
                xml.updateNodeElement(fileName, appName, "one", "");
                xml.updateNodeElement(fileName, appName, "two", "Control+Alt+S");
                xml.updateNodeElement(fileName, appName, "three", "Control+Alt+Left");
                xml.updateNodeElement(fileName, appName, "four", "Control+Alt+Right");
                xml.updateNodeElement(fileName, appName, "five", "Control+Alt+Up");
                xml.updateNodeElement(fileName, appName, "six", "Control+Alt+Down");
             }
            else if (str1=="exe")
            {
                xml.updateNodeElement(fileName, appName, "zero", "Up");
                xml.updateNodeElement(fileName, appName, "one", "Down");
                xml.updateNodeElement(fileName, appName, "two", "Left");
                xml.updateNodeElement(fileName, appName, "three", "Right");
                xml.updateNodeElement(fileName, appName, "four", "");
                xml.updateNodeElement(fileName, appName, "five", "");
                xml.updateNodeElement(fileName, appName, "six", "");
                xml.updateNodeElement(fileName, appName, "seven", "");
                xml.updateNodeElement(fileName, appName, "eight", "");
                xml.updateNodeElement(fileName, appName, "nine", "");
            }
            else
            {
                xml.updateNodeElement(fileName, appName, "zero", "Up");
                xml.updateNodeElement(fileName, appName, "one", "Down");
                xml.updateNodeElement(fileName, appName, "two", "Left");
                xml.updateNodeElement(fileName, appName, "three", "Right");
                xml.updateNodeElement(fileName, appName, "four", "Up");
                xml.updateNodeElement(fileName, appName, "five", "Down");
                xml.updateNodeElement(fileName, appName, "six", "Left");
                xml.updateNodeElement(fileName, appName, "seven", "Right");
                xml.updateNodeElement(fileName, appName, "eight", "Return");
                xml.updateNodeElement(fileName, appName, "nine", "Alt+F4");
            }
            loadDatagridviewData(appName);
        }

        

    }
}
